package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.Factura;

public class FacturaDAO {
	
	private static FacturaDAO instancia = null;
	private static SessionFactory sf = null;

	public static FacturaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new FacturaDAO();
		} 
		return instancia;
	}
	
	public void guardarFactura(Factura f){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(f);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarFactura(Factura f){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(f);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarFactura(Factura f){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(f);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Factura> obtenerFacturas(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Facturas");
		List<Factura> facturas = q.list();
		session.close();
		return facturas;
	}
}



